using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

namespace gammmaertld
{
    public class Game1 : Microsoft.Xna.Framework.Game
    {
        GraphicsDeviceManager graphics;
        SpriteBatch spriteBatch;
        Texture2D background, mBackground;
        Texture2D newMapUp, newMapDown;
        Texture2D exitUp, exitDown;

        //Navigation Help
        Texture2D saveButton, backButton, selection;

        Texture2D navUp, navDown, navLeft, navRight;
        Texture2D zoomIn, zoomOut, navSelection;

        //Terrain
        Texture2D terrain, tGrass, tRoad, tForest;
        Texture2D tMountain, tWater, tCoral;

        //Units
        Texture2D units, uGround, uAir, uWater;
        Texture2D[] gUnits, aUnits, wUnits;
        
        //Buildings
        Texture2D buildings;
        Texture2D[] buildList;

        //Booleans
        //isMain controls whether or not the program will enter the editor
        bool isMain       = true;

        //isEditor controls whether or not the program is in map-making mode
        bool isEditor     = false;

        //isTerrain controls the terrain menu
        bool isTerrain    = false;

        //isBuildings controls the building menu.
        bool isBuildings  = false;

        //isUnits controls the unit menu. unitMain has the options of ground, air and water.
        //unitGround/Air/Water have the units of each type
        bool isUnits      = false;
            bool unitMain    = true;
            bool unitGround  = false;
            bool unitAir     = false;
            bool unitWater   = false;

        //iSelection is used for menu items
        int iSelection    = 0;
        //nSelection is used for unit/building items
        int nSelection    = 0;
        //tSelection is used for terrain items
        int tSelection    = 0;

        public Game1()
        {
            graphics = new GraphicsDeviceManager(this);
            Components.Add(new InputManager(this));
            Content.RootDirectory = "Content";
            this.IsMouseVisible = true;
        }
        protected override void Initialize()
        {
            base.Initialize();
        }
        protected override void LoadContent()
        {
            spriteBatch = new SpriteBatch(GraphicsDevice);

            #region Textures for the main menu
            background = Content.Load<Texture2D>("Main\\Background");
            newMapUp = Content.Load<Texture2D>("Main\\NewMap_Up");
            newMapDown = Content.Load<Texture2D>("Main\\NewMap_Down");
            exitUp = Content.Load<Texture2D>("Main\\Exit_Up");
            exitDown = Content.Load<Texture2D>("Main\\Exit_Down");
            #endregion

            #region Textures for the editor
            mBackground = Content.Load<Texture2D>("Editor\\Background");
            backButton = Content.Load<Texture2D>("Editor\\Back");
            saveButton = Content.Load<Texture2D>("Editor\\Save");
            selection = Content.Load<Texture2D>("Editor\\Selection");

            #region Textrures for the navigation buttons
            navUp = Content.Load<Texture2D>("Editor\\Navigation\\Up");
            navDown = Content.Load<Texture2D>("Editor\\Navigation\\Down");
            navRight = Content.Load<Texture2D>("Editor\\Navigation\\Right");
            navLeft = Content.Load<Texture2D>("Editor\\Navigation\\Left");
            zoomIn = Content.Load<Texture2D>("Editor\\Navigation\\Zoom_In");
            zoomOut = Content.Load<Texture2D>("Editor\\Navigation\\Zoom_Out");
            navSelection = Content.Load<Texture2D>("Editor\\Navigation\\Selection");
            #endregion

            #endregion

            #region Textures for the terrain menu
            terrain = Content.Load<Texture2D>("Editor\\Terrain\\Terrain");
            tGrass = Content.Load<Texture2D>("Editor\\Terrain\\Grass");
            tForest = Content.Load<Texture2D>("Editor\\Terrain\\Forest");
            tMountain = Content.Load<Texture2D>("Editor\\Terrain\\Mountain");
            tRoad = Content.Load<Texture2D>("Editor\\Terrain\\Road");
            tWater = Content.Load<Texture2D>("Editor\\Terrain\\Water");
            tCoral = Content.Load<Texture2D>("Editor\\Terrain\\Coral");
            #endregion

            #region Textures for the unit menu
            units = Content.Load<Texture2D>("Editor\\Units\\Units");
            uGround = Content.Load<Texture2D>("Editor\\Units\\Ground\\Ground");
            uAir = Content.Load<Texture2D>("Editor\\Units\\Air\\Air");
            uWater = Content.Load<Texture2D>("Editor\\Units\\Water\\Water");
            gUnits = new Texture2D[5]
                {
                  Content.Load<Texture2D>("Editor\\Units\\Ground\\Worker"),
                  Content.Load<Texture2D>("Editor\\Units\\Ground\\AssaultMarine"),
                  Content.Load<Texture2D>("Editor\\Units\\Ground\\RocketMarine"),
                  Content.Load<Texture2D>("Editor\\Units\\Ground\\Tank"),
                  Content.Load<Texture2D>("Editor\\Units\\Ground\\Walker")
                };
            aUnits = new Texture2D[2]
                {
                  Content.Load<Texture2D>("Editor\\Units\\Air\\Helicopter"),
                  Content.Load<Texture2D>("Editor\\Units\\Air\\Bomber"),
                };
            wUnits = new Texture2D[2]
                {
                  Content.Load<Texture2D>("Editor\\Units\\Water\\Submarine"),
                  Content.Load<Texture2D>("Editor\\Units\\Water\\Battleship"),
                };
            #endregion

            //Eventually the building textures will be here, for now it is only the one meant for 
            buildings = Content.Load<Texture2D>("Editor\\Buildings\\Buildings");
        }
        protected override void Update(GameTime gameTime)
        {
            if (InputManager.IsKeyPressed(Keys.Escape))
                this.Exit();

            //If the program is in the main menu
            if (isMain)
            {
                if (InputManager.MouseXLocation >= 295 && InputManager.MouseXLocation <= 503)
                {
                    //If the mouse is over the button "New Map"
                    if (InputManager.MouseYLocation >= 167 && InputManager.MouseYLocation <= 207)
                    {
                        //Ready it to be selected
                        iSelection = 1;

                        //If the left mouse button is pressed over the button "New Map"
                        if (InputManager.IsLButtonTriggered)
                        {
                            //Open the editor and close the 
                            isEditor = true;
                            isMain = false;
                        }
                    }
                    //If the mouse is over the button "Exit"
                    else if (InputManager.MouseYLocation >= 225 && InputManager.MouseYLocation <= 265)
                    {
                        //Ready it to be selected
                        iSelection = 2;

                        //If the left mouse button is pressed over the button "Exit"
                        if (InputManager.IsLButtonTriggered)
                        {
                            //Exit the program
                            this.Exit();
                        }
                    }
                }
                else
                    iSelection = 0;
            }
            //If the program is in the editor menu
            else if (isEditor)
            {
                //Update the basic tools of the editor
                UpdateEditor();

                //If the program is in terrain menu
                if (isTerrain)
                    //Update the terrain menu
                    UpdateTerrain();

                //If the program is in buildings menu
                else if (isBuildings)
                    //Update the buildings menu
                    UpdateBuildings();

                //If the program is in units menu
                else if (isUnits)
                    //Update the units menu
                    UpdateUnits();
            }

            base.Update(gameTime);
        }
        //This function updates the editor menu basics, this is the submenu selecetions (save, terrain, buildings, units)
        //provided the submenus are not selected already. It also will update the navigation buttons, so that they
        //are always working and usable.
        public void UpdateEditor()
        {
            if (InputManager.MouseYLocation >= 540 && InputManager.MouseYLocation <= 583
                &&!isTerrain && !isBuildings && !isUnits)
            {
                #region Save button
                if (InputManager.MouseXLocation >= 228 && InputManager.MouseXLocation <= 271)
                {
                    iSelection = 1;
                }
                #endregion
                #region Terrain submenu
                if (InputManager.MouseXLocation >= 271 && InputManager.MouseXLocation <= 314)
                {
                    iSelection = 2;

                    if (InputManager.IsLButtonTriggered)
                    {
                        isTerrain = true;
                        isBuildings = false;
                        isUnits = false;
                    }
                }
                #endregion
                #region Buildings submenu
                if (InputManager.MouseXLocation >= 314 && InputManager.MouseXLocation <= 357)
                {
                    iSelection = 3;

                    if (InputManager.IsLButtonTriggered)
                    {
                        isBuildings = true;
                        isTerrain = false;
                        isUnits = false;
                    }
                }
                #endregion
                #region Units submenu
                if (InputManager.MouseXLocation >= 357 && InputManager.MouseXLocation <= 400)
                {
                    iSelection = 4;

                    if (InputManager.IsLButtonTriggered)
                    {
                        isUnits = true;
                        isTerrain = false;
                        isBuildings = false;
                    }
                }
                #endregion
            }
            else
                iSelection = 0;

            #region Navigation
            if (InputManager.MouseXLocation >= 749 && InputManager.MouseXLocation <= 759
             && InputManager.MouseYLocation >= 10 && InputManager.MouseYLocation <= 20)
                uSelection = 1;
            else if (InputManager.MouseXLocation >= 760 && InputManager.MouseXLocation <= 770
             && InputManager.MouseYLocation >= 21 && InputManager.MouseYLocation <= 31)
                uSelection = 2;
            else if (InputManager.MouseXLocation >= 749 && InputManager.MouseXLocation <= 759
             && InputManager.MouseYLocation >= 32 && InputManager.MouseYLocation <= 42)
                uSelection = 3;
            else if (InputManager.MouseXLocation >= 738 && InputManager.MouseXLocation <= 748
             && InputManager.MouseYLocation >= 21 && InputManager.MouseYLocation <= 31)
                uSelection = 4;
            else if (InputManager.MouseXLocation >= 773 && InputManager.MouseXLocation <= 783
             && InputManager.MouseYLocation >= 14 && InputManager.MouseYLocation <= 24)
                uSelection = 5;
            else if (InputManager.MouseXLocation >= 773 && InputManager.MouseXLocation <= 783
             && InputManager.MouseYLocation >= 28 && InputManager.MouseYLocation <= 38)
                uSelection = 6;
            else
                uSelection = 0;
            #endregion
        }
        public void UpdateTerrain()
        {
            if (InputManager.MouseYLocation >= 540 && InputManager.MouseYLocation <= 583)
            {
                #region Back
                if (InputManager.MouseXLocation >= 228 && InputManager.MouseXLocation <= 271)
                {
                    iSelection = 1;

                    if (InputManager.IsLButtonTriggered)
                    {
                        isTerrain = false;
                        isBuildings = false;
                        isUnits = false;
                    }
                }
                #endregion

                //In the following order are the terrain tiles:
                //Grass, Forest, Mountain, Road, Water, Coral
                #region Terrain
                if (InputManager.MouseXLocation >= 271 && InputManager.MouseXLocation <= 314)
                {
                    iSelection = 2;

                    if (InputManager.IsLButtonTriggered)
                        tSelection = 1;
                }
                if (InputManager.MouseXLocation >= 314 && InputManager.MouseXLocation <= 357)
                {
                    iSelection = 3;

                    if (InputManager.IsLButtonTriggered)
                        tSelection = 2;
                }
                if (InputManager.MouseXLocation >= 357 && InputManager.MouseXLocation <= 400)
                {
                    iSelection = 4;

                    if (InputManager.IsLButtonTriggered)
                        tSelection = 3;
                }
                if (InputManager.MouseXLocation >= 400 && InputManager.MouseXLocation <= 443)
                {
                    iSelection = 5;

                    if (InputManager.IsLButtonTriggered)
                        tSelection = 4;
                }
                if (InputManager.MouseXLocation >= 443 && InputManager.MouseXLocation <= 486)
                {
                    iSelection = 6;

                    if (InputManager.IsLButtonTriggered)
                        tSelection = 5;
                }
                if (InputManager.MouseXLocation >= 486 && InputManager.MouseXLocation <= 529)
                {
                    iSelection = 7;

                    if (InputManager.IsLButtonTriggered)
                        tSelection = 6;
                }
                #endregion
            }
            else
                iSelection = 0;

        }
        public void UpdateBuildings()
        {
            if (InputManager.MouseYLocation >= 540 && InputManager.MouseYLocation <= 583)
            {
                #region Back
                if (InputManager.MouseXLocation >= 228 && InputManager.MouseXLocation <= 271)
                {
                    iSelection = 1;

                    if (InputManager.IsLButtonTriggered)
                    {
                        isTerrain   = false;
                        isBuildings = false;
                        isUnits     = false;
                    }
                }
                #endregion
            }
            else
                iSelection = 0;
        }
        public void UpdateUnits()
        {
            if (InputManager.MouseYLocation >= 540 && InputManager.MouseYLocation <= 583)
            {
                #region uMain
                if (InputManager.MouseXLocation >= 228 && InputManager.MouseXLocation <= 271
                 && uMain)
                {
                    iSelection = 1;

                    if (InputManager.IsLButtonTriggered)
                    {
                        isTerrain = false;
                        isBuildings = false;
                        isUnits = false;
                    }
                }
                if (InputManager.MouseXLocation >= 271 && InputManager.MouseXLocation <= 314
                 && uMain)
                {
                    iSelection = 2;

                    if (InputManager.IsLButtonTriggered)
                    {
                        uGround = true;
                        uMain = false;
                        uWater = false;
                        uAir = false;
                    }
                }
                if (InputManager.MouseXLocation >= 314 && InputManager.MouseXLocation <= 357
                 && uMain)
                {
                    iSelection = 3;

                    if (InputManager.IsLButtonTriggered)
                    {
                        uAir = true;
                        uMain = false;
                        uGround = false;
                        uWater = false;
                    }
                }
                if (InputManager.MouseXLocation >= 357 && InputManager.MouseXLocation <= 400
                 && uMain)
                {
                    iSelection = 4;

                    if (InputManager.IsRButtonTriggered)
                    {
                        uWater = true;
                        uMain = false;
                        uGround = false;
                        uAir = false;
                    }
                }
                #endregion
                #region uGround
                if (InputManager.MouseXLocation >= 228 && InputManager.MouseXLocation <= 271
                 && uGround)
                {
                    iSelection = 1;

                    if (InputManager.IsLButtonTriggered)
                    {
                        uMain = true;
                        uGround = false;
                        uAir = false;
                        uWater = false;
                    }
                }
                if (InputManager.MouseXLocation >= 271 && InputManager.MouseXLocation <= 314
                 && uGround)
                {
                    iSelection = 2;

                    if (InputManager.IsLButtonTriggered)
                    {
                    }
                }
                if (InputManager.MouseXLocation >= 314 && InputManager.MouseXLocation <= 357
                 && uGround)
                {
                    iSelection = 3;

                    if (InputManager.IsLButtonTriggered)
                    {
                    }
                }
                if (InputManager.MouseXLocation >= 357 && InputManager.MouseXLocation <= 400
                 && uGround)
                {
                    iSelection = 4;

                    if (InputManager.IsLButtonTriggered)
                    {
                    }
                }
                if (InputManager.MouseXLocation >= 400 && InputManager.MouseXLocation <= 443
                 && uGround)
                {
                    iSelection = 5;

                    if (InputManager.IsLButtonTriggered)
                    {
                    }
                }
                if (InputManager.MouseXLocation >= 443 && InputManager.MouseXLocation <= 486
                 && uGround)
                {
                    iSelection = 6;

                    if (InputManager.IsLButtonTriggered)
                    {
                    }
                }
                #endregion
                #region uAir
                if (InputManager.MouseXLocation >= 228 && InputManager.MouseXLocation <= 271
                 && uAir)
                {
                    iSelection = 1;

                    if (InputManager.IsLButtonTriggered)
                    {
                        uMain = true;
                        uGround = false;
                        uAir = false;
                        uWater = false;
                    }
                }
                if (InputManager.MouseXLocation >= 271 && InputManager.MouseXLocation <= 314
                 && uAir)
                {
                    iSelection = 2;

                    if (InputManager.IsLButtonTriggered)
                    {
                    }
                }
                if (InputManager.MouseXLocation >= 314 && InputManager.MouseXLocation <= 357
                 && uAir)
                {
                    iSelection = 3;

                    if (InputManager.IsLButtonTriggered)
                    {
                    }
                }
                #endregion
                #region uWater
                if (InputManager.MouseXLocation >= 228 && InputManager.MouseXLocation <= 271
                 && uWater)
                {
                    iSelection = 1;

                    if (InputManager.IsLButtonTriggered)
                    {
                        uMain = true;
                        uGround = false;
                        uAir = false;
                        uWater = false;
                    }
                }
                if (InputManager.MouseXLocation >= 271 && InputManager.MouseXLocation <= 314
                 && uWater)
                {
                    iSelection = 2;

                    if (InputManager.IsLButtonTriggered)
                    {
                    }
                }
                if (InputManager.MouseXLocation >= 314 && InputManager.MouseXLocation <= 357
                 && uWater)
                {
                    iSelection = 3;

                    if (InputManager.IsLButtonTriggered)
                    {
                    }
                }
                #endregion
            }
            else
                iSelection = 0;
        }

        protected override void Draw(GameTime gameTime)
        {
            GraphicsDevice.Clear(Color.CornflowerBlue);
            spriteBatch.Begin();
            if (isMain)
            {
                spriteBatch.Draw(background, Vector2.Zero, Color.White);

                spriteBatch.Draw(newMapUp, new Vector2(295, 167), Color.White);
                if (iSelection == 1)
                    spriteBatch.Draw(newMapDown, new Vector2(295, 167), Color.White);

                spriteBatch.Draw(exitUp, new Vector2(295, 225), Color.White);
                if (iSelection == 2)
                    spriteBatch.Draw(exitDown, new Vector2(295, 225), Color.White);
            }
            else if (isEditor)
            {
                DrawEditor(spriteBatch);

                if (isTerrain)
                    DrawTerrain(spriteBatch);
                else if (isBuildings)
                    DrawBuildings(spriteBatch);
                else if (isUnits)
                    DrawUnits(spriteBatch);
            }

            spriteBatch.End();
            base.Draw(gameTime);
        }

        public void DrawEditor(SpriteBatch spritebatch)
        {
            spritebatch.Draw(mBackground, Vector2.Zero, Color.White);

            //In the following order are the main Editor options:
            // Save, Terrain, Buildings, Units
            if (!isTerrain && !isBuildings && !isUnits)
            {
                #region Options
                spriteBatch.Draw(saveButton, new Vector2(228, 540), Color.White);
                if (iSelection == 1)
                    spritebatch.Draw(selection, new Vector2(228, 540), Color.White);

                spriteBatch.Draw(terrain, new Vector2(271, 540), Color.White);
                if (iSelection == 2)
                    spritebatch.Draw(selection, new Vector2(271, 540), Color.White);

                spriteBatch.Draw(buildings, new Vector2(314, 540), Color.White);
                if (iSelection == 3)
                    spritebatch.Draw(selection, new Vector2(314, 540), Color.White);

                spriteBatch.Draw(units, new Vector2(357, 540), Color.White);
                if (iSelection == 4)
                    spritebatch.Draw(selection, new Vector2(357, 540), Color.White);
                #endregion
            }

            #region Navigation
            spriteBatch.Draw(navUp, new Vector2(749, 10), Color.White);
            if (uSelection == 1)
                spriteBatch.Draw(navSelection, new Vector2(749, 10), Color.White);

            spriteBatch.Draw(navRight, new Vector2(760, 21), Color.White);
            if (uSelection == 2)
                spriteBatch.Draw(navSelection, new Vector2(760, 21), Color.White);

            spriteBatch.Draw(navDown, new Vector2(749, 32), Color.White);
            if (uSelection == 3)
                spriteBatch.Draw(navSelection, new Vector2(749, 32), Color.White);

            spriteBatch.Draw(navLeft, new Vector2(738, 21), Color.White);
            if (uSelection == 4)
                spriteBatch.Draw(navSelection, new Vector2(738, 21), Color.White);

            spriteBatch.Draw(zoomIn, new Vector2(773, 14), Color.White);
            if (uSelection == 5)
                spriteBatch.Draw(navSelection, new Vector2(773, 14), Color.White);

            spriteBatch.Draw(zoomOut, new Vector2(773, 28), Color.White);
            if (uSelection == 6)
                spriteBatch.Draw(navSelection, new Vector2(773, 28), Color.White);
            #endregion
        }
        public void DrawTerrain(SpriteBatch spritebatch)
        {
            spriteBatch.Draw(backButton, new Vector2(228, 540), Color.White);
            if (iSelection == 1)
                spritebatch.Draw(selection, new Vector2(228, 540), Color.White);

            //In the following order are the terrain tiles:
            //Grass, Forest, Mountain, Road, Water, Coral
            #region Terrain
            spriteBatch.Draw(grass, new Vector2(271, 540), Color.White);
            if (iSelection == 2)
                spritebatch.Draw(selection, new Vector2(271, 540), Color.White);

            spriteBatch.Draw(forest, new Vector2(314, 540), Color.White);
            if (iSelection == 3)
                spritebatch.Draw(selection, new Vector2(314, 540), Color.White);

            spriteBatch.Draw(mountain, new Vector2(357, 540), Color.White);
            if (iSelection == 4)
                spritebatch.Draw(selection, new Vector2(357, 540), Color.White);

            spriteBatch.Draw(road, new Vector2(400, 540), Color.White);
            if (iSelection == 5)
                spritebatch.Draw(selection, new Vector2(400, 540), Color.White);

            spriteBatch.Draw(tWater, new Vector2(443, 540), Color.White);
            if (iSelection == 6)
                spritebatch.Draw(selection, new Vector2(443, 540), Color.White);

            spriteBatch.Draw(coral, new Vector2(486, 540), Color.White);
            if (iSelection == 7)
                spritebatch.Draw(selection, new Vector2(486, 540), Color.White);
            #endregion
        }
        public void DrawBuildings(SpriteBatch spritebatch)
        {
            spriteBatch.Draw(backButton, new Vector2(228, 540), Color.White);
            if (iSelection == 1)
                spritebatch.Draw(selection, new Vector2(228, 540), Color.White);
        }
        public void DrawUnits(SpriteBatch spritebatch)
        {
            spriteBatch.Draw(backButton, new Vector2(228, 540), Color.White);
            if (iSelection == 1)
                spritebatch.Draw(selection, new Vector2(228, 540), Color.White);
            #region uMain
            if (uMain)
            {
                spriteBatch.Draw(ground, new Vector2(271, 540), Color.White);
                if (iSelection == 2)
                    spritebatch.Draw(selection, new Vector2(271, 540), Color.White);

                spriteBatch.Draw(air, new Vector2(314, 540), Color.White);
                if (iSelection == 3)
                    spritebatch.Draw(selection, new Vector2(314, 540), Color.White);

                spriteBatch.Draw(water, new Vector2(357, 540), Color.White);
                if (iSelection == 4)
                    spritebatch.Draw(selection, new Vector2(357, 540), Color.White);
            }
            #endregion
            #region uGround
            if (uGround)
            {
                for (int i = 0; i < 5; i++)
                    spritebatch.Draw(gUnits[i], new Vector2(271 + (43 * i), 540), Color.White);
                if (iSelection == 2)
                    spritebatch.Draw(selection, new Vector2(271, 540), Color.White);
                if (iSelection == 3)
                    spritebatch.Draw(selection, new Vector2(314, 540), Color.White);
                if (iSelection == 4)
                    spritebatch.Draw(selection, new Vector2(357, 540), Color.White);
                if (iSelection == 5)
                    spritebatch.Draw(selection, new Vector2(400, 540), Color.White);
                if (iSelection == 6)
                    spritebatch.Draw(selection, new Vector2(443, 540), Color.White);
            }
            #endregion
            #region uAir
            if (uAir)
            {
                for (int i = 0; i < 2; i++)
                    spritebatch.Draw(aUnits[i], new Vector2(271 + (43 * i), 540), Color.White);
                if (iSelection == 2)
                    spritebatch.Draw(selection, new Vector2(271, 540), Color.White);
                if (iSelection == 3)
                    spritebatch.Draw(selection, new Vector2(314, 540), Color.White);
            }
            #endregion
            #region uWater
            if (uWater)
            {
                for (int i = 0; i < 2; i++)
                    spritebatch.Draw(wUnits[i], new Vector2(271 + (43 * i), 540), Color.White);
                if (iSelection == 2)
                    spritebatch.Draw(selection, new Vector2(271, 540), Color.White);
                if (iSelection == 3)
                    spritebatch.Draw(selection, new Vector2(314, 540), Color.White);
            }
            #endregion
        }
    }
}
